package syan.okhttp;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.time.Duration;

import static java.lang.System.out;

/**
 * @author CHENTAIWEN
 * @create 2022/9/22 10:47
 */
@Slf4j
public class okttpDemo {
    public static void main(String[] args) {
        String parBodyStr = "{page: 1, limit: 100, search: \"\", type: 100}";
        log.info(parBodyStr);
        OkHttpClient client = new OkHttpClient()
                .newBuilder()
                .connectTimeout(Duration.ofSeconds(30L))
                .writeTimeout(Duration.ofSeconds(30L))
                .readTimeout(Duration.ofSeconds(30L))
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, parBodyStr);
        Request request = new Request.Builder()
                .url("http://localhost:8325/repairs")
//                .method("POST", body)
                .get()
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            String json = response.body().string();
            log.info(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        out.println("-------------");
        Request request2 = new Request.Builder()
                .url("http://localhost:8325/repairs")
//                .method("POST", body)
                .method("GET", null)
                .addHeader("Content-Type", "application/json")
                .build();
        Response response2 = null;
        try {
            response2 = client.newCall(request2).execute();
            String json = response2.body().string();
            log.info(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
